﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WLMCoreLibrary;
using MiWebSite_Core;

namespace MWS_AddDirectory.MisAnuncios
{
    public partial class GenerarPago : System.Web.UI.Page
    {
        public dbConnector ConexionDB;
        public WebSiteMainConfiguration config;
        protected void Page_Load(object sender, EventArgs e)
        {
            config = WebSiteMainConfiguration.GetConfig();
            if (Request["idAnuncio"] != null)
                ProcesarIntencionPagoAnuncio(Convert.ToInt32(Request["idAnuncio"]));
            
        }

        protected void ProcesarIntencionPagoAnuncio(int idAnuncio)
        {
                ConexionDB = new dbConnector(config.Connections["WebSite"].value);
                try
                {
                    Anuncio anuncio = new Anuncio(idAnuncio, ref ConexionDB);
                    Cliente cliente = new Cliente(anuncio.idCliente, ref ConexionDB);
                    if (anuncio.id != 0)
                    {
                        AnuncioTransaccion transaccion = new AnuncioTransaccion(ref ConexionDB);
                        transaccion.idAnuncio = anuncio.id;
                        transaccion.idCliente = anuncio.idCliente;
                        transaccion.GUID = Guid.NewGuid().ToString();
                        transaccion.monto = 365;
                        transaccion.currency = "MXN";
                        transaccion.Concepto = "Pago de Anuncio No. " + anuncio.id + " - '" + anuncio.nombre + "' en Directorio de " + DeCurrentDomain.Ciudad;
                        transaccion.idPromotor = anuncio.idPromotor;
                        transaccion.PaymentProvider = PaymentProviders.oxxo;
                        transaccion.Save();
                        Payment pago = new Payment();
                        pago.GetPaymentTicketDineroMail(transaccion, anuncio, cliente);
                        
                        SuccessObject success = new SuccessObject();
                        success.Message = "GeneracionCompletada";
                        success.Code = SuccessCode.PaymentGenerated;
                        success.Object = WLMFunctions.SerializeObject(transaccion);
                        Response.ContentType = "text/xml";
                        Response.Write(WLMFunctions.SerializeObject(success, true));
                        
                    }

                }
                catch (Exception ex)
                {
                    ConexionDB.Error = true;
                    ErrorObject error = new ErrorObject(ErrorCode.PaymentError, ex.Message, ex.Message, ex.StackTrace);
                    Response.ContentType = "text/xml";
                        Response.Write(WLMFunctions.SerializeObject(error, true));

                }
            ConexionDB.CloseConnection();
            Response.End();
        }
    }
}